package com.xmy.cultivate.listeners;

import com.xmy.cultivate.constants.MqConstants;
import com.xmy.cultivate.entity.OrderDetail;
import com.xmy.cultivate.service.IOrderDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class OrderListenner {


    @Autowired
    IOrderDetailService iOrderDetailService;

    /**
     * 智能课或者刷题班自动生成
     * @param orderDetail
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_AUTO_ORDER_APPLY_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_AUTO_ORDER_APPLY_FANOUT, type = ExchangeTypes.FANOUT)
    ))
    public void autoOrderApply(OrderDetail orderDetail){
        iOrderDetailService.addOrderApply(orderDetail.getYearPart(),orderDetail.getQuarterNum(),orderDetail.getSchoolId(),orderDetail.getSubjectsId(),orderDetail.getLessonType(),orderDetail.getStudentId());
    }

    /**
     * 智能课或者刷题班更新剩余课时为零
     * @param orderDetail
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CULTIVATE_UPDATE_RESIDUE_COURSE_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CULTIVATE_UPDATE_RESIDUE_COURSE_FANOUT, type = ExchangeTypes.FANOUT)
    ))
    public void updateResidueCourse(OrderDetail orderDetail){
        iOrderDetailService.updateResidueCourse(orderDetail.getYearPart(),orderDetail.getQuarterNum(),orderDetail.getSchoolId(),orderDetail.getSubjectsId(),orderDetail.getLessonType(),orderDetail.getStudentId());
    }
}
